package lib_model.Product;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Tax_product_history implements ISql {

	public int id;
	public int tax_productid;
	public String description;
	public int accountusername;

	public Tax_product_history() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select tax_productid, description, accountusername from dbo.tax_product_history";
		ReturnObj obj = new ReturnObj();
		ArrayList<Tax_product_history> arr = new ArrayList<Tax_product_history>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Tax_product_history a = new Tax_product_history();
				a.tax_productid = rs.getInt("tax_productid");
				a.description = rs.getString("description");
				a.accountusername = rs.getInt("accountusername");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.tax_product_history (tax_productid,   description,   accountusername) values   (?,  ?,   ?)";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.tax_productid);
			preState.setString(2, this.description);
			preState.setInt(3, this.accountusername);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.tax_product_history set tax_productid = ?, escription = ?, accountusername = ? where tax_productid = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.description);
			preState.setInt(2, this.accountusername);
			preState.setInt(3, this.tax_productid);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.tax_product_history where tax_productid = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.tax_productid);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select tax_productid, description, accountusername from dbo.tax_product_history where tax_productid = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.tax_productid);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Tax_product_history a = new Tax_product_history();
				a.tax_productid = rs.getInt("tax_productid");
				a.description = rs.getString("description");
				a.accountusername = rs.getInt("accountusername");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}
}
